package com.lkd.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lkd.dto.TaskDTO;
import com.lkd.dto.TaskTypeDTO;
import com.lkd.entity.Task;
import com.lkd.entity.TaskStatusType;
import com.lkd.entity.TaskType;
import com.lkd.mapper.TaskMapper;
import com.lkd.mapper.TaskStatusTypeMapper;
import com.lkd.mapper.TaskTypeMapper;
import com.lkd.result.TableInfo;
import com.lkd.service.TaskService;
import com.lkd.vo.TaskReportVO;
import com.lkd.vo.TaskVO;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Z.lx
 * @Date: 2025/8/23 16:22
 * @Description:
 **/
@Service
@AllArgsConstructor
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {
    private final TaskMapper taskMapper;
    private final TaskStatusTypeMapper taskStatusTypeMapper;
    private final TaskTypeMapper taskTypeMapper;
    /**
    * @Description 取消工单
    * @Param [taskId, taskDTO]
    * @Date 2025/8/24 19:40
    **/
    public boolean cancel(Long taskId, TaskDTO taskDTO) {
        Task task = taskMapper.selectById(taskId);
        if(task != null && task.getStatus().equals(1)){
            task.setStatus(3);
            task.setDesc(taskDTO.getRemark());
            return taskMapper.updateById(task) > 0;
        }
        return false;
    }

    /**
    * @Description 查询工单
    * @Param [taskDTO]
    * @Date 2025/8/24 19:40
    **/
    public TableInfo<TaskVO> search(TaskDTO taskDTO) {
        PageHelper.startPage(taskDTO.getPageIndex(),taskDTO.getPageSize());
        Task task = new Task();
        BeanUtils.copyProperties(taskDTO,task);

        List<TaskVO> taskVOList = taskMapper.selectTaskVoList(task);

        Page<TaskVO> page = (Page<TaskVO>)taskVOList;
        return new TableInfo<>(taskDTO.getPageIndex(),taskDTO.getPageSize(),page.getPages(),page.getTotal(),taskVOList);
    }

    /**
    * @Description 查询所有工单状态
    * @Param []
    * @Date 2025/8/24 19:40
    **/
    public List<TaskStatusType> getAllStatus() {
        return taskStatusTypeMapper.selectList(null);
    }

    /**
    * @Description 查询所有工单类型
    * @Param [taskTypeDTO]
    * @Date 2025/8/24 19:51
    **/
    public List<TaskType> getAllType() {
        return taskTypeMapper.selectList( null);
    }

    @Override
    public List<TaskReportVO> getTaskReportInfo(String start, String end) {
        List<TaskReportVO> taskReportVOList = new ArrayList<>();
        List<LocalDate> dataList = formatTime(start, end);
        for (LocalDate date : dataList){
            TaskReportVO taskReportVO = new TaskReportVO();
            taskReportVO.setDate(date.toString());
            taskReportVO.setTotal(super.count());
            taskReportVO.setCompletedTotal(super.count(Wrappers.<Task>lambdaQuery().eq(Task::getStatus,4)));
            taskReportVO.setProgressTotal(super.count(Wrappers.<Task>lambdaQuery().eq(Task::getStatus,2)));
            taskReportVO.setCancelTotal(super.count(Wrappers.<Task>lambdaQuery().eq(Task::getStatus,3)));
            taskReportVO.setWorkerCount(super.count(Wrappers.<Task>lambdaQuery().eq(Task::getStatus,2)));
            taskReportVO.setRepair(true);
            taskReportVOList.add(taskReportVO);
        }
        return taskReportVOList;
    }
    public List<LocalDate> formatTime(String start, String end) {
        LocalDate startTime = LocalDateTimeUtil.parse(start, "yyyy-MM-dd HH:mm:ss").toLocalDate();
        LocalDate endTime = LocalDateTimeUtil.parse(end, "yyyy-MM-dd HH:mm:ss").toLocalDate();;
        List<LocalDate> dataList = new ArrayList<>();
        dataList.add(startTime);
        //循环，日期+1，添加到dateList
        while (!startTime.isEqual(endTime)) {
            startTime = startTime.plusDays(1);
            dataList.add(startTime);
        }
        return dataList;
    }
}
